#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>

/*
 * at24c02_test /dev/myat24c02 
 */

int main(int argc, char **argv)
{
	int fd,index;
	char buf[18];
	char rbuf[18];
	char wbuff[16];

	if (argc < 2)
	{
		printf("Usage: %s <dev>\n", argv[0]);
		return -1;
	}
	
	fd = open(argv[1], O_RDWR);
	if (fd < 0)
	{
		printf(" can not open %s\n", argv[1]);
		return -1;
	}

	printf("Writing...\n");
	for(index = 0; index < 16; index++)
	{
		wbuff[index] = index;
		buf[0] = 0;
		buf[1] = index;
		buf[2] = index;
		write(fd, buf, 3);
	}

	printf("Reading...\n");
	for(index = 0; index < 16; index++)
	{
		rbuf[0] = 0; 
		rbuf[1] = index; 
		read(fd, rbuf, 3); 
		if(rbuf[2] == wbuff[index])
			printf("Read[%d][%02x] = Write[%d][%02x]\n", index, rbuf[2], index, wbuff[index]);
		else
			printf("Err at [%d]\n", index);
	} 
	
	close(fd);
	return 0;
}


